package cdu.sl.aichatdemo.utils;

import java.util.Random;

public class CodeUtil {

    private static final String NUMBER_CHARS = "0123456789";
    private static final String ALPHA_NUMERIC_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final int CODE_LENGTH = 6;

    /**
     * 生成6位纯数字验证码
     * @return 6位数字验证码
     */
    public static String generateNumericCode() {
        return generateCode(NUMBER_CHARS);
    }

    /**
     * 生成6位数字+字母混合验证码
     * @return 6位数字字母混合验证码
     */
    public static String generateAlphaNumericCode() {
        return generateCode(ALPHA_NUMERIC_CHARS);
    }

    /**
     * 通用验证码生成方法
     *
     * @param chars 字符集
     * @return 随机验证码
     */
    private static String generateCode(String chars) {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < CodeUtil.CODE_LENGTH; i++) {
            code.append(chars.charAt(random.nextInt(chars.length())));
        }
        return code.toString();
    }


}